/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var maximumsSplicedArray = function(nums1, nums2) {
  let maxValue = Math.abs(nums1[0] - nums2[0]), maxLeft = 0, maxRight = 0, left = 0;
  function sum(payload) {
    let result = 0;
    for(let i = 0; i < payload.length; i++) {
      result += payload[i];
    }
    return result;
  }
  for(let i = 1; i < nums1.length; i++) {
    let tempLeft = sum(nums1.slice(left, i + 1)), tempRight = sum(nums2.slice(left, i + 1));
    let value = Math.abs(tempLeft - tempRight);
    if(value > maxValue) {
      maxRight = i;
      maxLeft = left;
      maxValue = value;
    } else {
      left = i;
      right = i;
    }
  }
  let temp1 = [...nums1], temp2 = [...nums2];
  temp1.splice(maxLeft, maxRight - maxLeft + 1);
  temp2.splice(maxLeft, maxRight - maxLeft + 1);
  console.log(nums1.slice(maxLeft, maxRight + 1).concat(temp2), nums2.slice(maxLeft, maxRight + 1).concat(temp1));
  const resultLeft = sum(nums1.slice(maxLeft, maxRight + 1).concat(temp2))
  const resultRight = sum(nums2.slice(maxLeft, maxRight + 1).concat(temp1))
  return Math.max(resultLeft, resultRight);
};
let nums1 = [60,60,60], nums2 = [10,90,10];
console.log(maximumsSplicedArray(nums1, nums2));
nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20];
console.log(maximumsSplicedArray(nums1, nums2));
nums1 = [7,11,13], nums2 = [1,1,1];
console.log(maximumsSplicedArray(nums1, nums2));
